/*******************************************************************************
 * Copyright (c) 2011 Olexandr V. Shaposhnikov.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Olexandr V. Shaposhnikov - initial API and implementation and/or initial documentation
 *******************************************************************************/
package com.funkyroach.use.udk;

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.dltk.core.environment.IDeployment;
import org.eclipse.dltk.core.environment.IFileHandle;
import org.eclipse.dltk.internal.launching.AbstractInterpreterInstallType;
import org.eclipse.dltk.launching.EnvironmentVariable;
import org.eclipse.dltk.launching.IInterpreterInstall;
import org.eclipse.dltk.launching.LibraryLocation;

import com.funkyroach.use.core.language.USNature;
import com.funkyroach.use.plugin.USPlugin;

public class USUDKInstallType extends AbstractInterpreterInstallType {
    private static final String INSTALL_TYPE_NAME = "UDK"; //$NON-NLS-1$
    private static final String[] INTERPRETER_NAMES = { "UDK" }; //$NON-NLS-1$
    private static final String UDK_x32 = "\\Binaries\\Win32\\";
    private static final String UDK_x64 = "\\Binaries\\Win64\\";

    public USUDKInstallType() {
        super();
    }

    public static int runUDK(String udkHome, boolean x64, StringBuffer sb, String... params) {
        USUDKLauncher launcher = new USUDKLauncher(udkHome, x64 ? UDK_x64 : UDK_x32);
        return launcher.runUDKexe(sb, params);
    }

    public static int retreiveVersion(String udkHome) {
        int version = -1;
        try {
            StringBuffer sb = new StringBuffer();
            if (runUDK(udkHome, false, sb, "Help") == 0) {
                String str = sb.toString();
                Scanner scaner = new Scanner(str).skip("Init: Version:");
                version = scaner.nextInt();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return version;
    }

    @Override
    public String getNatureId() {
        return USNature.NATURE_ID;
    }

    @Override
    protected IInterpreterInstall doCreateInterpreterInstall(String id) {
        return new USUDKInstall(this, id);
    }

    @Override
    protected String[] getPossibleInterpreterNames() {
        return INTERPRETER_NAMES;
    }

    @Override
    protected String getPluginId() {
        return USPlugin.PLUGIN_ID;
    }

    @Override
    protected ILog getLog() {
        return USPlugin.instance().getLog();
    }

    protected ILookupRunnable createLookupRunnable(final IFileHandle installLocation,
            final List<LibraryLocation> locations, final EnvironmentVariable[] variables) {

        return new ILookupRunnable() {
            public void run(IProgressMonitor monitor) {

            }
        };
    }

    // @Override
    // public synchronized LibraryLocation[] getDefaultLibraryLocations(
    // IFileHandle installLocation, EnvironmentVariable[] variables,
    // IProgressMonitor monitor) {
    // if (monitor != null)
    // monitor.beginTask("Default source", 1);
    // ArrayList<LibraryLocation> locations = new ArrayList<LibraryLocation>();
    // locations.add(new LibraryLocation(new Path(
    // "c:\\UDK\\UDK-2011-08\\Development\\Src")));
    // LibraryLocation[] libs = locations
    // .toArray(new LibraryLocation[locations.size()]);
    // if (monitor != null)
    // monitor.done();
    // return libs;
    // }

    @Override
    protected IPath createPathFile(IDeployment deployment) throws IOException {
        return null; //$NON-NLS-1$
    }

    @Override
    public String getName() {
        return INSTALL_TYPE_NAME;
    }

}
